package com.aube.plugin.gallery.preview;

import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;

import com.aube.plugin.gallery.PictureImp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by panwankun on 2016/1/21.
 */
public class ImagePreviewActivityHelper {

    public static final int LOCATION_TYPE_DEFAULT = 0;
    public static final int LOACTION_TYPE_MOVIEPOSTER_LIST = 1;
//    public static final int LOACTION_TYPE_MOVIEPOSTER = 2;
//    public static final int LOACTION_TYPE_PHOTOSELECT = 3;
//    public static final int LOACTION_TYPE_MOVIEPOSTER_SELECT = 4;

    public static void startPreivew(GalleryPreviewLayout previewLayout, View small, List<PictureImp> pictures, Bitmap originBitmap, int originIndex, int type){
        if(pictures == null || pictures.size() == 0)
            return;
        OriginBitmapHelper.getInstance().setOriginBitmap(originBitmap);
        previewLayout.showPreview(toPictures(small, pictures, type), originIndex);
    }

    private static ArrayList<ViewPicture> toPictures(View small, List<PictureImp> list,int type){
        ArrayList<ViewPicture> pictures = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            PictureImp pic = list.get(i);
            ViewPicture vp = new ViewPicture();
            vp.setHeight(pic.getHeight());
            vp.setPictureUrl(pic.getPictureUrl());
            vp.setWidth(pic.getWidth());
            vp.setDesc(pic.getDesc());

            vp.picid = pic.picid;
            vp.praiseCount = pic.praiseCount;
            vp.commentCount = pic.commentCount;
            vp.status = pic.status;

            View smallView = getChild(small,i,type);
            if (smallView != null) {
                int[] location = {0, 0};
                smallView.getLocationInWindow(location);
                vp.viewLeft = location[0];
                vp.viewTop = location[1];
                vp.viewWidth = smallView.getWidth();
                vp.viewHeight = smallView.getHeight();
//                System.out.println(i + "----------" + vp.viewLeft + "/" + vp.viewTop + "/" + vp.viewWidth + "/" + vp.viewHeight);
            }
            pictures.add(vp);
        }
        return pictures;
    }

    public static ArrayList<ViewPicture> toPictures(List<PictureImp> list){
        ArrayList<ViewPicture> pictures = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            PictureImp pic = list.get(i);
            ViewPicture vp = new ViewPicture();
            vp.setHeight(pic.getHeight());
            vp.setPictureUrl(pic.getPictureUrl());
            vp.setWidth(pic.getWidth());
            vp.setDesc(pic.getDesc());

            vp.picid = pic.picid;
            vp.praiseCount = pic.praiseCount;
            vp.commentCount = pic.commentCount;
            vp.status = pic.status;

            pictures.add(vp);
        }
        return pictures;
    }

    private static View getChild(View small,int position,int type){
        if (type == LOCATION_TYPE_DEFAULT){
            return getChild_default(small,position);
        }else if (type == LOACTION_TYPE_MOVIEPOSTER_LIST){
            return getChild_movieposter_list(small,position);
//        }else if (type == LOACTION_TYPE_MOVIEPOSTER_SELECT){
//            return getChild_movieposter_select(small,position);
        }else {
            return small;
        }
    }

    private static View getChild_default(View small,int position){
        return ((ViewGroup)small.getParent()).getChildAt(position);
    }

    private static View getChild_movieposter_list(View small,int position){
        try {
            return ((ViewGroup) small.getParent().getParent()).getChildAt(position + 1);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

//    private static View getChild_movieposter_select(View small,int position){
//        return ((ViewGroup)small.getParent().getParent()).getChildAt(position);
//    }
//
//    private static Rect getChild_photo_rect(Context context,int position ,int itemSize , int scrolly){
//        Rect rect = new Rect();
//        int count = position % 3 + 1 >= 3 ? 0 : position % 3 + 1;
//        rect.left = count * itemSize;
//        rect.top = ((position + 1) / 3) * itemSize + SysUtil.getActionBarHeight(context) + SysUtil.getStatusHeight(context) - scrolly;
//        rect.right = rect.left+itemSize;
//        rect.bottom = rect.top + itemSize;
//        return rect;
//    }
}
